﻿using MK;
using System;
using System.Drawing.Printing;
using System.IO;

namespace AutoEditUI
{
    internal class Printer
    {
        public static string gbmp { get; set; }
        public static string SaveAndPrint(System.Windows.Controls.Canvas mainWindowMainCanvas, string sNData, string mTMData, double MBLConfigureDPITimes, string MBLConfigurePicPath, bool MBLConfigureXpsPrintOn, bool MBLConfigureMFGOn, double w, double h)
        {

            string x = SaveSn(MBLConfigurePicPath, MBLConfigureDPITimes, mainWindowMainCanvas, sNData, mTMData);
            Print(x, mainWindowMainCanvas, MBLConfigureXpsPrintOn, MBLConfigureMFGOn, w, h);
            return x;
        }

        internal static string SaveSn(string MBLConfigurePicPath, double MBLConfigureDPITimes, System.Windows.Controls.Canvas mainCanvas, string sNData, string mTMData = "")
        {
            string pathMTM = MBLConfigurePicPath + @"\MTM";
            if (Directory.Exists(pathMTM) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(pathMTM);
            }
            string tmp = MBLConfigurePicPath + @"\" + sNData + ".bmp";


            if (!MBXUtils.ExportToPic(tmp, mainCanvas, MBLConfigureDPITimes))
            {
                LogHelper.Log("duplicate: " + sNData);
            }
            if (!string.IsNullOrWhiteSpace(mTMData))
            {
                string tm = pathMTM + @"\" + mTMData + ".bmp";
                if (!File.Exists(tm))
                {
                    File.Copy(tmp, tm, true);
                    LogHelper.Log("New MTM: " + sNData);
                }
            }
            return tmp;
        }

        private static void Print(string x, System.Windows.Controls.Canvas mainWindowMainCanvas, bool MBLConfigureXpsPrintOn, bool MBLConfigureMFGOn, double w, double h)
        { //加载图片到Image对象
            try
            {
                if (MBLConfigureXpsPrintOn)
                {
                    LogHelper.Log("XpsEx On Print Off");
                    XpsEx.GenerateXpsAndPdf(x, mainWindowMainCanvas);
                }
                if (MBLConfigureMFGOn || !MBLConfigureXpsPrintOn)
                {
                    LogHelper.Log("MFG PrintOn " + w + "wh" + h);
                    gbmp = x;
                    System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();//Refreshpd();//
                   // pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", (int)(w / 0.254), (int)(h / 0.254));
                    pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
                    pd.Print();
                }
            }
            catch (Exception ee)
            {
                LogHelper.Log("Print Exception" + ee.ToString());
            }
        }
        internal static void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            //  e.PageSettings.PaperSize = new PaperSize("First custom size", (int)(100 * 3.937008), (int)(30 * 3.937008));
            e.Graphics.DrawImage(System.Drawing.Image.FromFile(gbmp), 0, 0);
        }
    }
}